package com.sea.physical.controller;


import com.sea.physical.entity.StockCreatePlan;
import com.sea.physical.entity.StockCreateRecord;
import com.sea.physical.service.PortService;
import com.sea.physical.service.StockCreatePlanService;
import com.sea.physical.service.StockCreateRecordService;

import com.codecrane.core.Constants;
import com.codecrane.core.power.entity.PowerUser;
import com.codecrane.core.util.PhysicalStockStatusEnum;
import com.codecrane.core.web.Ajax;
import com.codecrane.core.web.AjaxReturn;
import com.codecrane.core.web.AjaxTableReturn;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping(value = "/sea/physical/stockCreate")
public class StockCreatePlanController {

    @Autowired
    private StockCreatePlanService stockCreatePlanService;

    @Autowired
    private StockCreateRecordService stockCreateRecordService;

    @Autowired
    private PortService portService;

    /**
     * 管理创建计划主页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/main")
    public String main(Model model) {
        return "/sea/physical/stockCreatePlan/main";
    }

    /**
     * 管理发运计划主页面
     * @param model
     * @return
     */
    @RequestMapping("/despatchMain")
    public String despatchMain(Model model){
        return "/sea/physical/stockDespatchPlan/main";
    }

    /**
     * 管理送货-入库计划主页面
     * @param model
     * @return
     */
    @RequestMapping("/customsMain")
    public String customMain(Model model,@RequestParam("createStatus")Integer createStatus){
        model.addAttribute("createStatus",createStatus-1);
        return "/sea/physical/stockCustomPlan/main";
    }
    /**
     * 新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/addpage")
    public String addpage(Model model) {
        getModel(model);
        return "/sea/physical/stockCreatePlan/add";
    }
    /**
     * 新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/editpage")
    public String editpage(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("id",id);
        return "/sea/physical/stockDespatchPlan/edit";
    }

    /**
     * 新增页面
     *
     * @param model
     * @return
     */
    @RequestMapping("/record")
    public String record(Model model,@RequestParam("id")Integer id) {
        model.addAttribute("id",id);
        return "/sea/physical/stockCreatePlan/record";
    }
    private void getModel(Model model) {
        //仓库字典
        model.addAttribute("ware",portService.getWareName());
    }
    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findByCnd")
    public AjaxTableReturn findByCnd(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                     @RequestParam(value = "offset", defaultValue = "0") int start,
                                     StockCreatePlan stockCreatePlan) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<StockCreatePlan> list = stockCreatePlanService.findByCnd(stockCreatePlan);
        PageInfo<StockCreatePlan> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }

    /**
     * 分页条件查询
     *
     * @param pagesize
     * @param start
     * @return
     */
    @ResponseBody
    @RequestMapping("/findRecord")
    public AjaxTableReturn findRecord(@RequestParam(value = "limit", defaultValue = "10") int pagesize,
                                      @RequestParam(value = "offset", defaultValue = "0") int start,
                                      @RequestParam("createId") Integer createId) {
        start = start > 0 ? start / pagesize + 1 : 1;
        PageHelper.startPage(start, pagesize);
        List<StockCreateRecord> list = stockCreateRecordService.findRecordByCreateId(createId);
        PageInfo<StockCreateRecord> page = new PageInfo<>(list);
        return Ajax.tableDataOk(page.getTotal(), page.getList());
    }
    /**
     * 保存
     *
     * @param stockCreatePlan
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxReturn save(StockCreatePlan stockCreatePlan) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            if (null != stockCreatePlan && stockCreatePlanService.save(stockCreatePlan) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
        }
        return result;
    }

    /**
     * 保存
     *
     * @param stockCreatePlan
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/usave", method = RequestMethod.POST)
    public AjaxReturn usave(StockCreatePlan stockCreatePlan) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            if (null != stockCreatePlan && stockCreatePlanService.usave(stockCreatePlan) > 0) {
                result.setOk(true).setMsg("保存成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
        }
        return result;
    }

    /**
     * 保存
     *
     * @param stockCreatePlan
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/updateStatus", method = RequestMethod.POST)
    public AjaxReturn updateStatus(StockCreatePlan stockCreatePlan, StockCreateRecord stockCreateRecord) {
        AjaxReturn result = Ajax.fail().setMsg("保存失败！");
        try {
            if (null != stockCreatePlan && stockCreatePlanService.usave(stockCreatePlan) > 0) {
                Subject subject = SecurityUtils.getSubject();
                PowerUser user = (PowerUser) subject.getSession().getAttribute(Constants.CURRENT_USER_SESSION);
                if (null != user) {
                    stockCreateRecord.setCreateOperateName(user.getUserNickname());
                }
                stockCreateRecord.setCreateStatus(stockCreatePlan.getCreateStatus());
                stockCreateRecord.setCreateRemark(PhysicalStockStatusEnum.getStatusName(stockCreatePlan.getCreateStatus()));
                stockCreateRecordService.save(stockCreateRecord);
                result.setOk(true).setMsg("保存成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.setMsg("系统异常,请联系管理员!");
        }
        return result;
    }
}
